﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace Nok_Air
{
    public partial class Report : PhoneApplicationPage
    {
        private string indexAvailabilityItemsPar = "";
        private string roundtripFlag = "";
        private string PassengerInfoPar = "";
        private string PaymentTypePar = "";
        private string RegisterIdPar = "";
        private int[] indexAvailabilityItems;
        private string[] sellingClass_indexAvailabilityItems;
        private string _FlightsXMLString = "";
        public Report()
        {
            InitializeComponent();
            DataContext = App.ViewModel;

            indexAvailabilityItems = new int[2];
            sellingClass_indexAvailabilityItems = new string[2];
            for (int aitemIndex = 0; aitemIndex < indexAvailabilityItems.Count(); aitemIndex++)
            {
                indexAvailabilityItems[aitemIndex] = -1;
                sellingClass_indexAvailabilityItems[aitemIndex] = "-1";
            }

        }

        private void continueButton_Click(object sender, RoutedEventArgs e)
        {
            if (Nok_Air.App.ViewModel.ReqisterPaymentChannelResult[0] == "200")
            {
                //            MessageBox.Show(App.ViewModel.PricingV2Items.PricingID);
                NavigationService.Navigate(new Uri("/BookFlight/5Form.xaml?FlightsXMLString=" + _FlightsXMLString + "&PaymentType=" + PaymentTypePar + "&RegisterId=" + RegisterIdPar + "&indexAvailabilityItems=" + indexAvailabilityItemsPar + "&PassengerInfo=" + PassengerInfoPar + "&roundtripFlag=" + roundtripFlag, UriKind.Relative));
            }
            else {
                MessageBox.Show(Nok_Air.App.ViewModel.ReqisterPaymentChannelResult[1]);
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e) 
        {
            base.OnNavigatedTo(e);
            if (NavigationService.CanGoBack && App.ViewModel.IsFinishTransaction)
            {
                NavigationService.GoBack();
            }

            if (NavigationContext.QueryString.TryGetValue("indexAvailabilityItems", out indexAvailabilityItemsPar))
            {
                if (indexAvailabilityItemsPar.Contains(','))
                {
                    string[] tmpPar = indexAvailabilityItemsPar.Split(',').ToArray();
                    indexAvailabilityItems[0] = int.Parse(tmpPar[0].Split('_')[0]);
                    indexAvailabilityItems[1] = int.Parse(tmpPar[1].Split('_')[0]);
                    sellingClass_indexAvailabilityItems[0] = tmpPar[0].Split('_')[1];
                    sellingClass_indexAvailabilityItems[1] = tmpPar[1].Split('_')[1];
                }
                else
                {
                    indexAvailabilityItems[0] = int.Parse(indexAvailabilityItemsPar.Split('_')[0]);
                    sellingClass_indexAvailabilityItems[0] = indexAvailabilityItemsPar.Split('_')[1];
                }
            }

            //get PassengerInfo parameter.
            NavigationContext.QueryString.TryGetValue("PassengerInfo", out PassengerInfoPar);
            //get PaymentType parameter.
            NavigationContext.QueryString.TryGetValue("PaymentType", out PaymentTypePar);
            //get RegisterId parameter.
            NavigationContext.QueryString.TryGetValue("RegisterId", out RegisterIdPar);

            NavigationContext.QueryString.TryGetValue("roundtripFlag", out roundtripFlag);
  

            /*             
<PricingV2 xmlns='http://www.nokair.com/webservices/'>
<Currency>THB</Currency> 
<Flights>
 <FlightInfo>
   <ClassCode>P</ClassCode>
   <FlightNumber>DD8300</FlightNumber>
   <FlightDate>04/07/2011</FlightDate>
   <FlightTime>07:35</FlightTime>
   <ArriveDate>04/07/2011</ArriveDate>
   <ArriveTime>08:45</ArriveTime>
   <OriginAirport>DMK</OriginAirport>
   <DestinationAirport>CNX</DestinationAirport>
 </FlightInfo>
 <FlightInfo>
   <ClassCode>W</ClassCode>
   <FlightNumber>DD8301</FlightNumber>
   <FlightDate>04/10/2011</FlightDate>
   <FlightTime>09:15</FlightTime>
   <ArriveDate>04/10/2011</ArriveDate>
   <ArriveTime>10:20</ArriveTime>
   <OriginAirport>CNX</OriginAirport>
   <DestinationAirport>DMK</DestinationAirport>
 </FlightInfo>
</Flights>
<PassengerInfos>
 <PassengerInfo>
   <PassengerCode>ADULT</PassengerCode>
   <NoOfSeats>2</NoOfSeats>
 </PassengerInfo>
 <PassengerInfo>
   <PassengerCode>CHD</PassengerCode>
   <NoOfSeats>1</NoOfSeats>
 </PassengerInfo>
</PassengerInfos>
<PaymentType>AT</PaymentType>
</PricingV2>
            */
            //call ws for get PricingV2. by create xml string first.
            string _pricingV2XMLString = "<Currency>THB</Currency>";
            _FlightsXMLString = "<Flights>";
            //App.ViewModel.AvailabilityItems[indexPivotItem].FlightSegmentItem[indexListBoxItem].ClassEconomy.ClassName;
            for (int i = 0; i < App.ViewModel.AvailabilityItems.Count(); i++)
            {

                string _nokEcoClassName = App.ViewModel.NokEco;
                string _nokFlexiClassName = App.ViewModel.NokFlexi;
                string _nokPlusClassName = App.ViewModel.NokPlus;

                string _sellingClassName = sellingClass_indexAvailabilityItems[i];

                string[] tmpFlightDateArr = App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].BoardDate.Split('-');
                //27-04-2011 to 04/27/2011
                string tmpFlightDate = string.Format("{1}/{0}/{2}", tmpFlightDateArr[2], tmpFlightDateArr[1], tmpFlightDateArr[0]);

                string[] tmpArriveDateArr = App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].BoardDate.Split('-');
                //27-04-2011 to 04/27/2011
                string tmpArriveDate = string.Format("{1}/{0}/{2}", tmpArriveDateArr[2], tmpArriveDateArr[1], tmpArriveDateArr[0]);

                    
                if (_sellingClassName == _nokEcoClassName)
                {
                   _FlightsXMLString += string.Format(
                       "<FlightInfo>" +
                        "<ClassCode>{0}</ClassCode>" +
                        "<FlightNumber>{1}</FlightNumber>" +
                        "<FlightDate>{2}</FlightDate>" +
                        "<FlightTime>{3}</FlightTime>" +
                        "<ArriveDate>{4}</ArriveDate>" +
                        "<ArriveTime>{5}</ArriveTime>" +
                        "<OriginAirport>{6}</OriginAirport>" +
                        "<DestinationAirport>{7}</DestinationAirport>" + 
                       "</FlightInfo>",
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ClassEconomy.ClassCode,
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].FlightNumber,
                        tmpFlightDate,
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureTime,
                        tmpArriveDate,
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalTime,
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureAirport,
                        App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalAirport
                        );
                }
                else if (_sellingClassName == _nokFlexiClassName)
                {
                    _FlightsXMLString += string.Format(
                      "<FlightInfo>" +
                       "<ClassCode>{0}</ClassCode>" +
                       "<FlightNumber>{1}</FlightNumber>" +
                       "<FlightDate>{2}</FlightDate>" +
                       "<FlightTime>{3}</FlightTime>" +
                       "<ArriveDate>{4}</ArriveDate>" +
                       "<ArriveTime>{5}</ArriveTime>" +
                       "<OriginAirport>{6}</OriginAirport>" +
                       "<DestinationAirport>{7}</DestinationAirport>" +
                      "</FlightInfo>",
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ClassFlexi.ClassCode,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].FlightNumber,
                       tmpFlightDate,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureTime,
                       tmpArriveDate,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalTime,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureAirport,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalAirport
                       );
                }
                else if (_sellingClassName == _nokPlusClassName)
                {
                    _FlightsXMLString += string.Format(
                      "<FlightInfo>" +
                       "<ClassCode>{0}</ClassCode>" +
                       "<FlightNumber>{1}</FlightNumber>" +
                       "<FlightDate>{2}</FlightDate>" +
                       "<FlightTime>{3}</FlightTime>" +
                       "<ArriveDate>{4}</ArriveDate>" +
                       "<ArriveTime>{5}</ArriveTime>" +
                       "<OriginAirport>{6}</OriginAirport>" +
                       "<DestinationAirport>{7}</DestinationAirport>" +
                      "</FlightInfo>",
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ClassNokPlus.ClassCode,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].FlightNumber,
                       tmpFlightDate,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureTime,
                       tmpArriveDate,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalTime,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].DepartureAirport,
                       App.ViewModel.AvailabilityItems[i].FlightSegmentItem[indexAvailabilityItems[i]].ArrivalAirport
                       );
                }
            }
            _FlightsXMLString += "</Flights>";
            _pricingV2XMLString += _FlightsXMLString + "<PassengerInfos>";

            string[] _PassengerInfoParArr = PassengerInfoPar.Split(',');
            if (!_PassengerInfoParArr[0].Equals("ADULT=0"))
            {
                _pricingV2XMLString += string.Format("<PassengerInfo>" +
                              "<PassengerCode>{0}</PassengerCode>" +
                              "<NoOfSeats>{1}</NoOfSeats>" +
                              "</PassengerInfo>", _PassengerInfoParArr[0].Split('=')[0], _PassengerInfoParArr[0].Split('=')[1]);
            }
            if (!_PassengerInfoParArr[1].Equals("CHD=0"))
            {
                _pricingV2XMLString += string.Format("<PassengerInfo>" +
                              "<PassengerCode>{0}</PassengerCode>" +
                              "<NoOfSeats>{1}</NoOfSeats>" +
                              "</PassengerInfo>", _PassengerInfoParArr[1].Split('=')[0], _PassengerInfoParArr[1].Split('=')[1]);
            }
            _pricingV2XMLString += "</PassengerInfos>";

            _pricingV2XMLString += string.Format("<PaymentType>{0}</PaymentType>", PaymentTypePar);

            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.PricingCompleted += (s, pe) =>
            {
                Dispatcher.BeginInvoke(() => {
                    if (pe.exception == null)
                        progressbar.IsOpen = false;
                    else if (NavigationService.CanGoBack && MessageBox.Show("Connection Problem","",MessageBoxButton.OK) == MessageBoxResult.OK) NavigationService.GoBack();
                });
            };
            nokSHelp.PricingV2(_pricingV2XMLString);
        }
    }
}